package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.JiajiaItem;
import com.ruoyi.system.domain.JiajiaWorker;
import com.ruoyi.system.domain.JiajiaWorkerItem;
import com.ruoyi.system.mapper.JiajiaItemMapper;
import com.ruoyi.system.mapper.JiajiaWorkerItemMapper;
import com.ruoyi.system.mapper.JiajiaWorkerMapper;
import com.ruoyi.system.service.IJiajiaWorkerItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 社工项目Service业务层处理
 *
 * @author ruoyi
 * @date 2021-10-13
 */
@Service
public class JiajiaWorkerItemServiceImpl implements IJiajiaWorkerItemService
{
    @Resource
    private JiajiaWorkerItemMapper jiajiaWorkerItemMapper;

    @Resource
    private JiajiaWorkerMapper workerMapper;

    @Resource
    private JiajiaItemMapper itemMapper;

    /**
     * 查询社工项目
     *
     * @param id 社工项目主键
     * @return 社工项目
     */
    @Override
    public JiajiaWorkerItem selectJiajiaWorkerItemById(Long id)
    {
        return jiajiaWorkerItemMapper.selectJiajiaWorkerItemById(id);
    }

    /**
     * 查询社工项目列表
     *
     * @param jiajiaWorkerItem 社工项目
     * @return 社工项目
     */
    @Override
    public List<JiajiaWorkerItem> selectJiajiaWorkerItemList(JiajiaWorkerItem jiajiaWorkerItem)
    {
        return jiajiaWorkerItemMapper.selectJiajiaWorkerItemList(jiajiaWorkerItem);
    }

    /**
     * 新增社工项目
     *
     * @param jiajiaWorkerItem 社工项目
     * @return 结果
     */
    @Override
    public AjaxResult insertJiajiaWorkerItem(JiajiaWorkerItem jiajiaWorkerItem) {
        JiajiaWorker jiajiaWorker = workerMapper.selectRepeat(jiajiaWorkerItem.getwName(),ShiroUtils.getSysUser().getLoginName());
        if (!Optional.ofNullable(jiajiaWorker).isPresent()){
            return AjaxResult.error("社工不存在");
        }
        if (!Optional.ofNullable(jiajiaWorkerItem.getWid()).isPresent()){
            jiajiaWorkerItem.setWid(jiajiaWorker.getId());
        }
        JiajiaItem jiajiaItem =itemMapper.selectName(jiajiaWorkerItem.getiName());
        if (!Optional.ofNullable(jiajiaItem).isPresent()){
            return AjaxResult.error("项目不存在");
        }
        if ( !Optional.ofNullable(jiajiaWorkerItem.getIid()).isPresent()){
            jiajiaWorkerItem.setIid(jiajiaItem.getId());
        }
        jiajiaWorkerItem.setStatus(Integer.valueOf(Constants.FAIL));
        int i = jiajiaWorkerItemMapper.insertJiajiaWorkerItem(jiajiaWorkerItem);
        System.out.println(i);
        return AjaxResult.success("添加成功");
    }


    @Override
    public void insertApiJiajiaWorkerItem(JiajiaWorkerItem jiajiaWorkerItem) {

        jiajiaWorkerItem.setStatus(Integer.valueOf(Constants.FAIL));
        jiajiaWorkerItemMapper.insertJiajiaWorkerItem(jiajiaWorkerItem);

    }

    /**
     * 修改社工项目
     *
     * @param jiajiaWorkerItem 社工项目
     * @return 结果
     */
    @Override
    public int updateJiajiaWorkerItem(JiajiaWorkerItem jiajiaWorkerItem)
    {
        return jiajiaWorkerItemMapper.updateJiajiaWorkerItem(jiajiaWorkerItem);
    }

    /**
     * 批量删除社工项目
     *
     * @param ids 需要删除的社工项目主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaWorkerItemByIds(String ids)
    {
        return jiajiaWorkerItemMapper.deleteJiajiaWorkerItemByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除社工项目信息
     *
     * @param id 社工项目主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaWorkerItemById(Long id)
    {
        return jiajiaWorkerItemMapper.deleteJiajiaWorkerItemById(id);
    }

    @Override
    public List<JiajiaWorkerItem> selectWorkerItemList(Long bigid) {
        return jiajiaWorkerItemMapper.selectWorkerItemList(bigid);
    }
}
